package yannan.voiplock4ble.utils;

import android.text.TextUtils;
import android.util.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Byte操作工具类
 */
public class ByteUtils {

    /**
     * 合并数组
     *
     * @param byte_1
     * @param byte_2
     * @return
     */
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    /**
     * 数组校验
     *
     * @param bytes
     * @return
     */
    public static byte[] getByteCheck(byte[] bytes) {

        byte s = 0x00;
        for (byte b : bytes) {
            System.out.println(b & 0xff);
            s = (byte) (s + b & 0xff);
        }
        byte[] a = {s};
        return a;
    }

    /**
     * 校验返回的内容
     *
     * @param bytes
     * @return
     */
    public static boolean byteCheck(byte[] bytes) {
        byte s = 0x00;
        for (int i = 0; i < bytes.length - 1; i++) {
            s = (byte) (s + bytes[i] & 0xff);
        }
        if (bytes[bytes.length - 1] == s) {
            return true;
        }
        return false;
    }

    /**
     * 16进制字符串转byte数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToByte(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            System.out.println(hexChars[i]);
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static int big2small(int i) {
        String test = Integer.toHexString(i);
        if (test.length() / 2 != 0) {
            test = "0" + test;
        }
        byte[] b = hexStringToByte(test);
        int a = Integer.parseInt(bytesToHexString(new byte[]{b[1], b[0]}), 16);
        return a;
    }

    /**
     * byte数组转换成int 小端存储模式四位Byte数组
     * 如byte[] test2 = new byte[]{0x39,0x1D,0x46,0x58}
     * int值为1480990009
     * @param res
     * @return
     */
    public static int byte2int(byte[] res) {
        // 一个byte数据左移24位变成0x??000000，再右移8位变成0x00??0000

        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表示安位或
                | ((res[2] << 24) >>> 8) | (res[3] << 24);
        return targets;
    }

    /**
     * 字符转为byte
     *
     * @param c
     * @return
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 数组转换成十六进制字符串
     *
     * @param bArray
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * byte转16进制字符串
     *
     * @param b
     * @return
     */
    public static String byte2HexString(byte b) {
        return Integer.toHexString(b & 0xff).toUpperCase();
    }

    /**
     * 字符串日期转 16进制byte数组
     * 例如 2016年09月12日10点07分50秒
     * 结果 14 10 09 0C 0A 07 32
     *
     * @param date 传进日期格式yyyyMMddHHmmss
     * @return
     */
    public static byte[] dateToHexStr(String date) {
        int i = 0;
        StringBuffer dateHexStr = new StringBuffer();
        while (i <= 6) {
            String hex = Integer.toHexString(Integer.parseInt(date.substring(i * 2, (i + 1) * 2)));
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            dateHexStr.append(hex);
            i++;
        }
        return hexStringToByte(dateHexStr.toString());
    }

    /**
     * byte数组转16进制字符串
     *
     * @param paramArrayOfByte
     * @param paramBoolean
     * @return
     */
    public static String bytesToHexString(byte[] paramArrayOfByte, boolean paramBoolean) {
        if ((paramArrayOfByte == null) || (paramArrayOfByte.length <= 0))
            return null;
        StringBuffer localStringBuffer = new StringBuffer(paramArrayOfByte.length);
        for (int i = 0; i < paramArrayOfByte.length; i++) {
            String str = Integer.toHexString(0xFF & paramArrayOfByte[i]);
            if (str.length() < 2)
                localStringBuffer.append(0);
            localStringBuffer.append(str);
            if (!paramBoolean)
                continue;
            localStringBuffer.append(" ");
        }
        return localStringBuffer.toString();
    }

    public static byte[] command2Bytes(String paramString) {
        byte[] arrayOfByte = null;
        try {
            boolean bool = TextUtils.isEmpty(paramString);
            arrayOfByte = null;
            if (!bool) {
                arrayOfByte = Base64.decode(paramString, 0);
            }
            return arrayOfByte;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return arrayOfByte;
    }

    public static byte[] getBytes(char paramChar) {
        byte[] arrayOfByte = new byte[1];
        arrayOfByte[0] = (byte) paramChar;
        return arrayOfByte;
    }

    public static byte[] intByte(int paramInt) {
        byte[] arrayOfByte = new byte[4];
        arrayOfByte[0] = (byte) (paramInt & 0xFF);
        arrayOfByte[1] = (byte) (0xFF & paramInt >> 8);
        arrayOfByte[2] = (byte) (0xFF & paramInt >> 16);
        arrayOfByte[3] = (byte) (paramInt >>> 24);
        return arrayOfByte;
    }

    public static String md5(File paramFile) {
        if ((paramFile != null) && (paramFile.isFile()))
            try {
                MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
                FileInputStream localFileInputStream = new FileInputStream(paramFile);
                byte[] arrayOfByte = new byte[1024];
                while (true) {
                    int i = localFileInputStream.read(arrayOfByte);
                    if (i <= 0)
                        break;
                    localMessageDigest.update(arrayOfByte, 0, i);
                }
                String str = bytesToHexString(localMessageDigest.digest(), false);
                return str;
            } catch (Exception localException) {
            }
        return null;
    }

    public static String md5(String paramString) {
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramString.getBytes());
            String str = bytesToHexString(localMessageDigest.digest(), false);
            return str;
        } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
        }
        return String.valueOf(paramString.hashCode());
    }

    public static byte[] shortByte(int paramInt) {
        byte[] arrayOfByte = new byte[2];
        arrayOfByte[0] = (byte) (paramInt & 0xFF);
        arrayOfByte[1] = (byte) (0xFF & paramInt >> 8);
        return arrayOfByte;
    }

    public static long strToDate(String paramString1, String paramString2) {
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2);
        try {
            long l = localSimpleDateFormat.parse(paramString1).getTime();
            return l;
        } catch (ParseException localParseException) {
            localParseException.printStackTrace();
        }
        return 0L;
    }
}

/* Location:           C:\Users\lenovo\Desktop\果加\classes_dex2jar.jar
 * Qualified Name:     cn.igoplus.locker.utils.ByteUtils
 * JD-Core Version:    0.6.0
 */